﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Autoupdate.Common
{
    public partial class MarqueeProgressForm : Form
    {
        public delegate void DoBackgroundWorkEventHandler(object sender, EventArgs e);

        public event DoBackgroundWorkEventHandler DoBackgroundWork;

        public MarqueeProgressForm()
        {
            InitializeComponent();

            backgroundWorker.DoWork += BackgroundWorker_DoWork;
            backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
            backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            label1.Text = this.Text;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            try
            {
                Util.Delay(200);
                backgroundWorker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                //ExceptionForm.ProcessException("", ex);
                MessageBox.Show(ex.Message + ex.StackTrace,
                    "错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }        
        }

        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                DoBackgroundWork?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                //ExceptionForm.ProcessException("", ex);
                MessageBox.Show(ex.Message + ex.StackTrace,
                    "错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                
            }
            catch (Exception ex)
            {
                //ExceptionForm.ProcessException("", ex);
                MessageBox.Show(ex.Message + ex.StackTrace,
                    "错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {               
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                //ExceptionForm.ProcessException("", ex);
                MessageBox.Show(ex.Message + ex.StackTrace,
                    "错误",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }
    }
}
